<?php

$Offer    = (object)$data['Offer'];
$Client   = (object)$data['Client'];
$Campaign = (object)$data['Campaign'];
$Tax      = (object)$data['Tax'];
$Settings = (object)$settings;

$dbLocation = new Location;

?>

<?= $this->Html->css('print', 'stylesheet', array('media'=>'print', 'inline'=>false)) ?>
<?= $this->Html->css('pdf', 'stylesheet', array('inline'=>false)) ?>
<?= $this->Html->script('footer.asset', array('inline'=>false)) ?>

<?= $this->Form->button('Štampa', array('onclick' => 'window.print()')) ?>

<header>
    <?= $this->Html->image('skyboard_print.png', array('width'=>240)) ?>
    <h1><?= $Client->name ?></h1>
</header>

<? $total = array(
    'price'            => 0,
    'discount'         => 0,
    'production_price' => 0,
    'neto_price'       => 0,
    'tax'              => 0,
    'total'            => 0
) ?>

<table>
    <tr class="blue">
        <th colspan="9">Zakup</th>
    </tr>
    <tr>
        <th align="left">Medij</th>
        <th align="center" width="10%">Period Zakupa</th>
        <th align="right" width="10%">Cena/mes. (<?= CURRENCY_SIGN ?>)</th>
        <th align="center" width="2%">Br.mes.</th>
        <th align="right" width="10%">Cena</th>
        <th align="right" width="10%">Popust (%)</th>
        <th align="right" width="10%">Neto Cena</th>
        <th align="right" width="10%">Porez (<?= $Tax->value ?>%)</th>
        <th align="right" width="10%">Ukupno</th>
    </tr>
    <? foreach ($data['OffersLocation'] as $k=>$loc): ?>
    <?
        $location      = $data['Location'][$k];
        if (empty($location['price'])) {
            $location['Location'] = $location;
            $location['price'] = $dbLocation->calculatePrice($location);
        }
        $locationPrice   = $location['rent_price'];
    
        $price           = $loc['price'];
        $discount_p      = (float)$loc['discount'];
        $discount        = $price * $discount_p / 100;
        $tax             = ($price - $discount) * $Tax->value / 100;
        $tot             = $price - $discount + $tax;
        
        
        $total['price']      += (float)$price;
        $total['discount']   += $discount;
        $total['neto_price'] += $price - $discount;
        $total['tax']        += (float)$tax;
        $total['total']      += (float)$tot;
    ?>
    <tr>
        <td align="left"><?= $loc['Location']['name'] ?><br><?= $loc['Location']['address'] ?></td>
        <td align="center"><?= date_local($loc['start']) ?><br><?= date_local($loc['end']) ?></td>
        <td align="right"><?= currency_format($locationPrice) ?></td>
        <td align="center"><?= $loc['num_months'] ?></td>
        <td align="right"><?= currency_format($price) ?></td>
        <td align="right"><?= currency_format($discount) ?> (<?= $discount_p ?>%)</td>
        <td align="right"><?= currency_format($price - $discount) ?></td>
        <td align="right"><?= currency_format($tax) ?></td>
        <td align="right"><?= currency_format($tot) ?></td>
    </tr>
    <? endforeach ?>
    <tr class="total">
        <td colspan="4" align="right">UKUPNO:</td>
        <td align="right"><?= currency_format($total['price']) ?></td>
        <td align="right"><?= currency_format($total['discount']) ?></td>
        <td align="right"><?= currency_format($total['neto_price']) ?></td>
        <td align="right"><?= currency_format($total['tax']) ?></td>
        <td align="right"><?= currency_format($total['total']) ?></td>
    </tr>
</table>

<? foreach ($data['Location'] as $k=>$loc) {
    $prods = array(PRODUCTION_GRATIS, PRODUCTION_NONE);
    if (in_array($loc['OffersLocation']['production'], $prods)) {
        unset($data['Location'][$k]);
    }
} ?>

<? $total2 = array(
    'm2'            => 0,
    'price_by_unit' => 0,
    'price'         => 0,
    'tax'           => 0,
    'total'         => 0
) ?>
<? if(! empty($data['Location'])): ?>
<table>
    <tr class="blue">
        <th colspan="7">Montaža</th>
    </tr>
    <tr>
        <th align="left">Medij</th>
        <th align="left" width="20%">Dimenzije</th>
        <th align="right" width="15%">m<sup>2</sup></th>
        <th align="right">Cena po jed.mere</th>
        <th align="right">Cena (<?= CURRENCY_SIGN ?>)</th>
        <th align="right">Porez (<?= $Tax->value ?>%)</th>
        <th align="right">Ukupno (<?= CURRENCY_SIGN ?>)</th>
    </tr>

    <? foreach ($data['Location'] as $loc): ?>
    <tr>
        <td align="left"><?= $loc['name'] ?><br><?= $loc['address'] ?></td>
        <td align="left">
            <?php
                $out         = array();
                $m2          = 0;
                $priceByType = array();
                $price       = array();
                
                $m2single    = 0;
                $priceByUnit = 0;
                $prc         = 0;
                $tax         = 0;
                $tot         = 0;
                
                if (empty($loc['price'])) {
                    $loc['price']          = $dbLocation->calculatePrice($loc);
                    $loc['price_location'] = $dbLocation->calculatePriceArray($loc);
                }
                
                $m2 = array();
                foreach ($loc['AdType'] as $k=>$adType) {
                    $w = $adType['LocationsAdType']['width'];
                    $h = $adType['LocationsAdType']['height'];
                    $name = $adType['name'];
                    $out[] =  "{$name}, {$w} x {$h} " . LENGTH_SYM;

                    $m2single1 = $w * $h;
                    $m2single += $m2single1;
                    $m2[]  = number_format($m2single1, 2, ",", ".");
                    
                    $prc1 = (float)$loc['price_location'][$k];
                    $tax1 = $prc1 * $Tax->value / 100;

                    $priceByUnit1 = $adType['price'];
                    $priceByUnit += $priceByUnit1;
                    $prc         += $prc1;
                    $tax         += $tax1;
                    $tot         += $prc1 + $tax1;
                    
                    $priceByType[] = $priceByUnit1 . ' ' . CURRENCY_SIGN . "/" . $adType['unit'];
                    $price[] = currency_format($prc);

                    $price_location = array();
                    foreach ($loc['price_location'] as $m=>$p) {
                        $price_location[$m] = currency_format($p);
                    }
                }

                $total2['m2']            = $m2single;
                $total2['price_by_unit'] = $priceByUnit;
                $total2['price']         += $prc;
                $total2['tax']           += $tax;
                $total2['total']         += $tot;
                echo implode('<br>', $out);
            ?>
        </td>
        <td align="right"><?= implode("<br>", $m2) ?></td>
        <td align="right"><?= implode("<br>", $priceByType) ?></td>
        <td align="right"><?= implode("<br>", $price_location) ?></td>
        <td align="right"><?= currency_format($tax) ?></td>
        <td align="right"><?= currency_format($tot) ?></td>
    </tr>
    <? endforeach ?>
    <tr class="total">
        <td colspan="4" align="right">UKUPNO:</td>
        <td align="right"><?= currency_format($total2['price']) ?></td>
        <td align="right"><?= currency_format($total2['tax']) ?></td>
        <td align="right"><?= currency_format($total2['total']) ?></td>
    </tr>
</table>
<? endif ?>

<table class="right">
    <tr class="blue">
        <th align="right">Neto:</th>
        <th align="right"><?= currency_format($total['price'] - $total['discount'] + $total2['price']) ?> <?= CURRENCY_SIGN ?></th>
    </tr>
    <tr class="blue">
        <th align="right"><?= $Tax->name ?>:</th>
        <th align="right"><?= currency_format($total['tax'] + $total2['tax']) ?> <?= CURRENCY_SIGN ?></th>
    </tr>
    <tr class="blue">
        <th align="right">UKUPNO:</th>
        <th align="right"><?= currency_format($total['total'] + $total2['total']) ?> <?= CURRENCY_SIGN ?></th>
    </tr>
</table>

<br clear="all">
<footer>
    <p><strong class="blue"><?= $Settings->name ?></strong></p>
    <p>
        <?= $Settings->address ?> &middot;
        <?= $Settings->phone ?> &middot;
        PIB: <?= $Settings->pib ?> &middot;
        MB: <?= $Settings->matbr ?>
    </p>
</footer>